package leetcode;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2021/12/1 8:53
 */
public class offer29 {


    @Test
    public void test() {
        int m = 3, n = 3;
        int[][] arr = new int[m][n];
        int k = 1;
        for (int i = 0; i < m; i++) {
            for (int i1 = 0; i1 < n; i1++) {
                arr[i][i1] = k;
                k++;
            }
        }
        spiralOrder(arr);
        System.out.println(arr);
    }


    public int[] spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < matrix.length; i++) {
            int m = matrix[i].length;
            if (i == 0) {
                for (int i1 = 0; i1 < m; i1++) {
                    list.add(matrix[i][i1]);
                }
            } else if (i == matrix.length - 1) {
                for (int i1 = m - 1; i1 >= 0; i1--) {
                    list.add(matrix[i][i1]);
                }
            } else {
                for (int i1 = 1; i1 < m - 1; i1++) {
                    list.add(matrix[i1][m - 1]);
                }
            }


        }
        int[] a = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            a[i] = list.get(i);
        }
        return a;
    }

}
